'----------------------------------------------
' Name: Ali Karbassi & Chris Hanegraaf
' Date: Date the project is created
' Description: Assignment 5
'              Form frmOrders
'----------------------------------------------
Public Class frmOrders

   ''' <summary>
   ''' Back button
   ''' </summary>
   ''' <param name="sender"></param>
   ''' <param name="e"></param>
   ''' <remarks></remarks>
   Private Sub btnBack_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
                                                                                    Handles btnBack.Click
      Me.Hide()
      frmUser.Show()
   End Sub

   ''' <summary>
   ''' Sets the table views.
   ''' </summary>
   ''' <param name="sender"></param>
   ''' <param name="e"></param>
   ''' <remarks></remarks>
   Private Sub frmOrders_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _
                                                                                       Handles MyBase.Load

      tblOrders.TableName = "Orders"
      ordersView.Table = tblOrders

      tblOrderDet.TableName = "Order Details"
      orderDetView.Table = tblOrderDet

      OrdersBindingSource.DataSource = tblOrders
      OrdersGrid.DataSource = ordersView

      OrderDetBindingSource.DataSource = tblOrderDet
      OrderDetGrid.DataSource = orderDetView

   End Sub

   ''' <summary>
   ''' All Orders radio button
   ''' </summary>
   ''' <param name="sender"></param>
   ''' <param name="e"></param>
   ''' <remarks></remarks>
   Private Sub rdoAllOrders_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) _
                                                                        Handles rdoAllOrders.CheckedChanged

      If rdoAllOrders.Checked Then

         txtID.DataBindings.Clear()
         txtName.DataBindings.Clear()
         txtID.Text = ""
         txtName.Text = ""
         btnPrev.Enabled = False
         btnNext.Enabled = False

         ' Retrieve all records
         ordersView.RowFilter = ""

      End If

   End Sub

   ''' <summary>
   ''' Updates the Order Detail gridview when Orders Grid cell is selected.
   ''' </summary>
   ''' <param name="sender"></param>
   ''' <param name="e"></param>
   ''' <remarks></remarks>
   Private Sub OrdersGrid_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) _
                                                                        Handles OrdersGrid.SelectionChanged

      Dim ID As Integer

      Try
         If Me.BindingContext(ordersView).Position <> -1 Then

            ID = ordersView(Me.BindingContext(ordersView).Position)(0)
            orderDetView.RowFilter = "OrderID = " & ID
         Else

            orderDetView.RowFilter = "OrderID = -1"

         End If

      Catch ex As Exception

         orderDetView.RowFilter = "OrderID = -1"

      End Try

   End Sub

   ''' <summary>
   ''' Customer radio button.
   ''' </summary>
   ''' <param name="sender"></param>
   ''' <param name="e"></param>
   ''' <remarks></remarks>
   Private Sub rdoCustomer_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) _
                                                                           Handles rdoCustomer.CheckedChanged

      If rdoCustomer.Checked Then

         ordersView.RowFilter = ""
         txtID.DataBindings.Clear()
         txtName.DataBindings.Clear()

         ' Select the first department
         Me.BindingContext(tblCust).Position = 0

         btnPrev.Enabled = False
         If Me.BindingContext(tblCust).Count > 0 Then
            btnNext.Enabled = True
         Else
            btnNext.Enabled = False
         End If

         txtID.DataBindings.Add("text", tblCust, "AccountNo")
         txtName.DataBindings.Add("text", tblCust, "Name")
         ordersView.RowFilter = "CustomerID = " & CInt(txtID.Text)

      End If

   End Sub

   ''' <summary>
   ''' Employee radio button.
   ''' </summary>
   ''' <param name="sender"></param>
   ''' <param name="e"></param>
   ''' <remarks></remarks>
   Private Sub rdoEmployee_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) _
                                                                        Handles rdoEmployee.CheckedChanged

      If rdoEmployee.Checked Then

         ordersView.RowFilter = ""
         txtID.DataBindings.Clear()
         txtName.DataBindings.Clear()

         ' Select the first department
         Me.BindingContext(tblEmp).Position = 0

         btnPrev.Enabled = False
         If Me.BindingContext(tblEmp).Count > 0 Then
            btnNext.Enabled = True
         Else
            btnNext.Enabled = False
         End If

         txtID.DataBindings.Add("text", tblEmp, "EmployeeID")
         txtName.DataBindings.Add("text", tblEmp, "Name")
         ordersView.RowFilter = "EmployeeID = " & CInt(txtID.Text)

      End If
   End Sub

   ''' <summary>
   ''' Closes the application when the 'x' button is clicked.
   ''' </summary>
   ''' <param name="sender"></param>
   ''' <param name="e"></param>
   ''' <remarks></remarks>
   Private Sub frmOrders_FormClosing(ByVal sender As System.Object, _
                           ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing

      Application.Exit()

   End Sub

   ''' <summary>
   ''' Previous button.
   ''' </summary>
   ''' <param name="sender"></param>
   ''' <param name="e"></param>
   ''' <remarks></remarks>
   Private Sub btnPrev_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrev.Click

      btnPrev.Enabled = True
      btnNext.Enabled = True

      If rdoCustomer.Checked Then

         Me.BindingContext(tblCust).Position -= 1
         ordersView.RowFilter = "CustomerID = " & CInt(txtID.Text)

         If Me.BindingContext(tblCust).Position = 0 Then
            btnPrev.Enabled = False
         End If

      ElseIf rdoEmployee.Checked Then

         Me.BindingContext(tblEmp).Position -= 1
         ordersView.RowFilter = "EmployeeID = " & CInt(txtID.Text)

         If Me.BindingContext(tblEmp).Position = 0 Then
            btnPrev.Enabled = False
         End If

      End If

   End Sub

   ''' <summary>
   ''' Next button.
   ''' </summary>
   ''' <param name="sender"></param>
   ''' <param name="e"></param>
   ''' <remarks></remarks>
   Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click

      btnPrev.Enabled = True
      btnNext.Enabled = True

      If rdoCustomer.Checked Then

         Me.BindingContext(tblCust).Position += 1
         ordersView.RowFilter = "CustomerID = " & CInt(txtID.Text)

         If Me.BindingContext(tblCust).Position = Me.BindingContext(tblCust).Count - 1 Then
            btnNext.Enabled = False
         End If

      ElseIf rdoEmployee.Checked Then

         Me.BindingContext(tblEmp).Position += 1
         ordersView.RowFilter = "EmployeeID = " & CInt(txtID.Text)

         If Me.BindingContext(tblEmp).Position = Me.BindingContext(tblEmp).Count - 1 Then
            btnNext.Enabled = False
         End If

      End If

   End Sub

   ''' <summary>
   ''' Saves the orders.
   ''' </summary>
   ''' <param name="sender"></param>
   ''' <param name="e"></param>
   ''' <remarks></remarks>
   Private Sub btnSaveOrders_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
                                                                                 Handles btnSaveOrders.Click

      Try
         'tblOrders.AcceptChanges()
         Me.BindingContext(tblOrders).EndCurrentEdit()
         ordersAdapter.Update(tblOrders)
      Catch ex As Exception
         MsgBox(ex.Message)
         tblOrders.RejectChanges()
         OrdersGrid.Refresh()
      End Try


   End Sub

   ''' <summary>
   ''' Saves the order details.
   ''' </summary>
   ''' <param name="sender"></param>
   ''' <param name="e"></param>
   ''' <remarks></remarks>
   Private Sub btnSaveOrderDetails_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
                                                                           Handles btnSaveOrderDetails.Click

      Try
         'tblOrderDet.AcceptChanges()
         Me.BindingContext(tblOrderDet).EndCurrentEdit()
         orderDetAdapter.Update(tblOrderDet)
      Catch ex As Exception
         MsgBox(ex.Message)
         tblOrderDet.RejectChanges()
         OrderDetGrid.Refresh()
      End Try

   End Sub

End Class